package com.lgh.enpu.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    private static String nowStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
    private static long nowLong = System.currentTimeMillis();
    public static String[] weekDays = new String[] {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};

    public static SimpleDateFormat getFormatYMDHMS() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public static SimpleDateFormat getFormatHMS() {
        return new SimpleDateFormat("HH:mm:ss");
    }

    public static SimpleDateFormat getFormatYMD() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public static String getWeekInZhcn(Date date) {
        return weekDays[getCalendar(date).get(7) - 1];
    }

    public static Calendar getCalendar(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c;
    }

    public static String getDString(long million) {
        Date d = new Date();
        d.setTime(million);
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(d);
    }

    public static String getNowDString() {
        long now = System.currentTimeMillis();
        if (now - nowLong >= 1000L) {
            nowStr = getNowDString("yyyy-MM-dd HH:mm:ss");
            nowLong = now;
        }
        return nowStr;
    }

    public static String getNowDString(String fmt) {
        return getNowDString(new SimpleDateFormat(fmt));
    }

    public static String getNowDString(SimpleDateFormat sdf) {
        return sdf.format(new Date());
    }

    public static String getNowDString(long time) {
        return getDString(System.currentTimeMillis() + time);
    }

    public static String getBirthdayDString(int age) {
        Calendar c = Calendar.getInstance();
        c.add(1, -age);
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(c.getTime());
    }

    public static Integer getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return Integer.valueOf(cal.get(1));
    }

    public static Integer getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return Integer.valueOf(cal.get(2) + 1);
    }

    public static Integer getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(5);
        return Integer.valueOf(day);
    }

    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(2);
        c.setTime(date);
        c.set(7, c.getFirstDayOfWeek());
        return c.getTime();
    }

    public static Date getLastDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(2);
        c.setTime(date);
        c.set(7, c.getFirstDayOfWeek() + 6);
        return c.getTime();
    }

    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(1, year);
        c.set(2, 0);
        c.set(5, 1);
        Calendar cal = (GregorianCalendar)c.clone();
        cal.add(5, week * 7);
        return getFirstDayOfWeek(cal.getTime());
    }

    public static Date getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(1, year);
        c.set(2, 0);
        c.set(5, 1);
        Calendar cal = (GregorianCalendar)c.clone();
        cal.add(5, week * 7);
        return getLastDayOfWeek(cal.getTime());
    }

    public static Date getFirstDayOfMonth(int year, int month) {
        month--;
        Calendar c = Calendar.getInstance();
        c.set(1, year);
        c.set(2, month);
        int day = c.getActualMinimum(5);
        c.set(5, day);
        c.set(11, 0);
        c.set(12, 0);
        c.set(13, 0);
        c.set(14, 0);
        return c.getTime();
    }

    public static Date getLastDayOfMonth(int year, int month) {
        month--;
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(1, year);
        c.set(2, month);
        int day = c.getActualMaximum(5);
        c.set(5, day);
        c.set(11, 23);
        c.set(12, 59);
        c.set(13, 59);
        c.set(14, 999);
        return c.getTime();
    }

    public static Long getDistanceDays(String str1, String str2, String fmt) throws Exception {
        DateFormat df = new SimpleDateFormat(fmt);
        long days = 0L;
        try {
            long diff;
            Date one = df.parse(str1);
            Date two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / 86400000L;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Long.valueOf(days);
    }

    public static int getWeekOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(4);
        int day = calendar.get(7);
        if (day == 1)
            week--;
        date = getFirstDayOfWeek(date);
        day = calendar.get(7);
        if (day == 1)
            week++;
        return week;
    }


    public static void main(String[] args) {
        System.out.println(DateUtils.getNowDString());
    }
}
